import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:app/generated/app_strings.dart';

import '../wroks/contract_bean.dart';

// {
// "ContractInfo": null,
// "CustomersInfo": null,
// "OrderInfo": null,
// "ContractPaymentTermsCopy": null,
// "ContractFinalPaymentCopy": null,
// "SpecialClausesList": null,
// "OrderItemList": null,
// "FileList": null,
// "EvidenceList": null,
// "ProductList": null,
// "OemAuditingList": null,
// "OemAuditingProductList": null,
// "IsUseOemAuditing": null,
// "AddressList": null,
// "ChangeFactoryId": null,
// "ChangeFactoryName": null,
// "DeliveryInfo": null,
// "DeliveryList": null,
// "PayFreeReturn": null,
// "PayFreeReturnList": null,
// "PayFreeGoodReturnList": null,
// "PayFreeReturnInfoList": null,
// "Receipt": null,
// "ReceiptItemList": null,
// "ReceiptDetails": null,
// "PayNoticeInfo": null,
// "PayNoticeDetails": null,
// "PayFreeInfo": null,
// "PayFreeDetails": null,
// "BackToUserInfoList": null,
// "PicList": null,
// "CarFreight": null,
// "OtherWorkApply": null,
// "GuaranteedReview": null,
// "WorkReview": null,
// "WorkOvertimeApprovals": null,
// "PercentageCheck": null,
// "ContractRoyaltyRateApplyCheck": {
// "ContractCode": "HT20221227001",
// "SignDate": "2022-12-27 0:00:00",
// "CustomerName": "#!恒发幕墙-柳启旺",
// "CompanyName": "宁波江花玻璃科技有限公司",
// "MerchandiserName": "袁华",
// "SalesmanName": "",
// "ProjectName": "#!恒发幕墙-柳启旺的直接下单合同",
// "OutCode": "XN167",
// "ContractContent": "",
// "MerchandiserRoyaltyRate": "1.001",
// "SalesmanRoyaltyRate": "14.006",
// "ModifyContractRoyaltyRate": {
// "ContractCode": "HT20221227001",
// "MerchandiserRoyaltyRateApply": "1.001",
// "SalesmanRoyaltyRateApply": "14.006",
// "MerchandiserRoyalty": [{
// "ContractCode": "HT20221227001",
// "PersonId": "10059",
// "PersonName": "卢灵永",
// "Ratio": "10.0000"
// }, {
// "ContractCode": "HT20221227001",
// "PersonId": "10065",
// "PersonName": "庄勇",
// "Ratio": "3.0000"
// }, {
// "ContractCode": "HT20221227001",
// "PersonId": "10857",
// "PersonName": "测试人员",
// "Ratio": "1.0000"
// }, {
// "ContractCode": "HT20221227001",
// "PersonId": "10981",
// "PersonName": "nine",
// "Ratio": "0.0060"
// }]
// }
// },
// "NodeList": [{
// "UserName": "测试人员",
// "YwxName": "发起合同提成比例",
// "Message": "测试",
// "InsertDatetime": "2023-02-14 16:30:14"
// }],
// "MsgCode": "100",
// "MsgDetail": "获取提成比例信息成功"
// }

///提成比例审核详情
class ContractRoyaltyRateApplyCheckBean extends ContractCopyInfo {
  //MerchandiserRoyaltyRate 客服提成
  String? MerchandiserRoyaltyRate = "";

  //SalesmanRoyaltyRate 业务提成；fixme
  String? SalesmanRoyaltyRate = "";

  //ModifyContractRoyaltyRate 提成比例实体类
  ModifyContractRoyaltyRateBean? ModifyContractRoyaltyRate = null;

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    super.fromJson(json); //fixme 先实现父类的
    MerchandiserRoyaltyRate = json['MerchandiserRoyaltyRate']?.toString() ?? "";
    SalesmanRoyaltyRate = json['SalesmanRoyaltyRate']?.toString() ?? "";
    ModifyContractRoyaltyRate = setGenericFromJson(
        json, "ModifyContractRoyaltyRate", ModifyContractRoyaltyRateBean(), "");
  }

  @override
  instance() {
    // TODO: implement instance
    return ContractRoyaltyRateApplyCheckBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = super.toJson(); //fixme 先实现父类的
    //var map = <String, dynamic>{};
    map["MerchandiserRoyaltyRate"] = MerchandiserRoyaltyRate;
    map["SalesmanRoyaltyRate"] = SalesmanRoyaltyRate;
    map["ModifyContractRoyaltyRate"] = ModifyContractRoyaltyRate;
    return map;
  }
}

///修改提成比例
class ModifyContractRoyaltyRateBean extends BaseBean {
  //合同编号
  String? ContractCode = "";

  //客服提成
  String? MerchandiserRoyaltyRateApply = "";

  //总业务提成
  String? SalesmanRoyaltyRateApply = "";

  String? getSalesmanRoyaltyRateApply() {
    double Ratio = 0;
    try {
      //总业务提示是合计出来的。
      MerchandiserRoyalty?.forEach((element) {
        // print(
        //     "========================================${Ratio}\t${element.Ratio}\t${double.parse(element.Ratio)}");
        //Ratio = Ratio + double.parse(element.Ratio);
        var str = DecimalUtils.add(Ratio.toString(), element.Ratio.toString())
            .toString();
        Ratio = double.parse(str);
        //print("=========================================$Ratio");
      });
      SalesmanRoyaltyRateApply = NumUtil.parse(Ratio); //防止数字过大或过小，以科学技术的形式显示。
    } catch (e) {}
    return SalesmanRoyaltyRateApply;
  }

  //修改提成比例里的实体类
  List<MerchandiserRoyaltyBaen>? MerchandiserRoyalty = null;

  List<MerchandiserRoyaltyBaen>? _OldMerchandiserRoyalty = null;

  ///获取提成比例详情分配文本
  String? getMerchandiserRoyaltyContent(BuildContext context) {
    if (_OldMerchandiserRoyalty != null &&
        _OldMerchandiserRoyalty!.length > 0) {
      var content = "";
      _OldMerchandiserRoyalty?.forEach((element) {
        if (content.length > 0) {
          content += "\n";
        }
        content +=
            "${element.PersonName}\t${AppStrings.of(context).payrate_}:${element.Ratio}%";
      });
      return content;
    }
    return null;
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    ContractCode = json['ContractCode']?.toString() ?? "";
    MerchandiserRoyaltyRateApply =
        json['MerchandiserRoyaltyRateApply']?.toString() ?? "";
    SalesmanRoyaltyRateApply =
        json['SalesmanRoyaltyRateApply']?.toString() ?? "";
    MerchandiserRoyalty = setGenericFromJson(json, "MerchandiserRoyalty",
        <MerchandiserRoyaltyBaen>[], MerchandiserRoyaltyBaen());
    if (MerchandiserRoyalty != null && MerchandiserRoyalty!.length > 0) {
      _OldMerchandiserRoyalty = [];
      MerchandiserRoyalty?.forEach((element) {
        _OldMerchandiserRoyalty?.add(element.copy());
      });
    }
  }

  @override
  instance() {
    // TODO: implement instance
    return ModifyContractRoyaltyRateBean();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["ContractCode"] = ContractCode;
    map["MerchandiserRoyaltyRateApply"] = MerchandiserRoyaltyRateApply;
    map["SalesmanRoyaltyRateApply"] = SalesmanRoyaltyRateApply;
    map["MerchandiserRoyalty"] = MerchandiserRoyalty;
    return map;
  }
}

///修改提成比例
class MerchandiserRoyaltyBaen extends BaseBean {
  //合同编号
  String? ContractCode = "";

  //人员id
  String? PersonId = "";

  //人员名称
  String? PersonName = "";

  //提成比例
  String Ratio = "";

  MerchandiserRoyaltyBaen copy() {
    MerchandiserRoyaltyBaen bean = MerchandiserRoyaltyBaen();
    bean.ContractCode = ContractCode;
    bean.PersonId = PersonId;
    bean.PersonName = PersonName;
    bean.Ratio = Ratio;
    return bean;
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    ContractCode = json['ContractCode']?.toString() ?? "";
    PersonId = json['PersonId']?.toString() ?? "";
    PersonName = json['PersonName']?.toString() ?? "";
    Ratio = json['Ratio']?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return MerchandiserRoyaltyBaen();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["ContractCode"] = ContractCode;
    map["PersonId"] = PersonId;
    map["PersonName"] = PersonName;
    map["Ratio"] = Ratio;
    return map;
  }
}

///送货路线
// "ContractFreightInfoList": [{
// "FreightId": 0,
// "ContractCode": null,
// "FactoryId": 0,
// "FactoryName": "江花玻璃（庄桥）",
// "AddressId": 0,
// "Address": "(工厂)宁波市鄞州区浙大宁波理工学院",
// "Distance": "23.40公里",
// "UserId": 0,
// "AddDate": "0001-01-01T00:00:00",
// "IsDel": 0,
// "CarNo": null,
// "AddressType": null
// }],
class ContractFreightInfo extends BaseBean {
  String? FreightId = "";
  String? ContractCode = "";
  String? FactoryId = "";
  String FactoryName = "";
  String AddressId = "";

  String Address = "";
  String Distance = "";
  String UserId = "";
  String AddDate = "";
  String IsDel = "";
  String CarNo = "";
  String AddressType = "";

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    FreightId = json['FreightId']?.toString() ?? "";
    ContractCode = json['ContractCode']?.toString() ?? "";
    FactoryId = json['FactoryId']?.toString() ?? "";
    FactoryName = json['FactoryName']?.toString() ?? "";
    AddressId = json['AddressId']?.toString() ?? "";

    Address = json['Address']?.toString() ?? "";
    Distance = json['Distance']?.toString() ?? "";
    UserId = json['UserId']?.toString() ?? "";
    AddDate = json['AddDate']?.toString() ?? "";
    IsDel = json['IsDel']?.toString() ?? "";
    CarNo = json['CarNo']?.toString() ?? "";
    AddressType = json['AddressType']?.toString() ?? "";
  }

  @override
  instance() {
    // TODO: implement instance
    return ContractFreightInfo();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["FreightId"] = FreightId;
    map["ContractCode"] = ContractCode;
    map["FactoryId"] = FactoryId;
    map["FactoryName"] = FactoryName;
    map["AddressId"] = AddressId;
    map["Address"] = Address;
    map["Distance"] = Distance;
    map["UserId"] = UserId;
    map["AddDate"] = AddDate;
    map["IsDel"] = IsDel;
    map["CarNo"] = CarNo;
    map["AddressType"] = AddressType;
    return map;
  }
}
